#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, char* argv[])
{
	if(argc<2) {
                printf("usages: %s <file to lock>\n", argv[0]);
                return -1;
        }

        pid_t pid = fork();
        if(pid!=0) {
		int fd;
		FILE *fp;
		struct flock filelock;
	 
		fp = fopen(argv[1], "r+b");

		filelock.l_type = F_WRLCK;
		filelock.l_whence = SEEK_SET;
		filelock.l_start = 0;
		filelock.l_len = 0;
		fd = fileno(fp);

		fcntl(fd, F_SETLKW, &filelock);

		printf("Locked By Pa\n");
		sleep(5);
		fclose(fp);
		printf("Released By Pa\n");
	 
		exit(0);
	}
	else {
		int fd;
		FILE *fp;
		struct flock filelock;

		fp = fopen(argv[1], "r+b");

		filelock.l_type = F_WRLCK;
		filelock.l_whence = SEEK_SET;
		filelock.l_start = 0;
		filelock.l_len = 0;
		fd = fileno(fp);

		fcntl(fd, F_SETLKW, &filelock);

		printf("Locked By Pb\n");
		sleep(5);
		fclose(fp);
		printf("Released By Pb\n");

		exit(0);
	}
}


